package com.example.administrator.privatetest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.privatetest.R;
import com.example.administrator.privatetest.util.Globals;

import java.util.List;
import java.util.Map;

public class MyAdapter extends BaseAdapter {

    //设定一一上下文和一个放map的容器
    private Context ctx;
    private List<Map<String,Object>> values;

    //初始化他们两个
    public MyAdapter(Context ctx, List<Map<String, Object>> values){
        this.ctx = ctx;
        this.values = values;
    }


    @Override
    public int getCount() {
        return values.size();
    }

    @Override
    public Object getItem(int i) {
        return values.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if(view==null){
            view = LayoutInflater.from(ctx).inflate(R.layout.file_list_line,null);
            view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    Globals.SCREEN_HEIGHT/8));
        }

        //设置map集合，用来接收从MainActivity中的values中传来的值，
        // 这时的map就是MainActivity中的map
        Map<String,Object> map = values.get(i);
        //初始化listview中的图片和文件名
        TextView fileImg = (TextView) view.findViewById(R.id.file_img);
        TextView fileName = (TextView) view.findViewById(R.id.file_name);


        //从map中取出文件
        fileName.setText(map.get("fileName").toString());
        //根据文件的名称来安放文件图片
        fileImg.setBackgroundResource(Globals.images.get(map.get("extName")));
        fileImg.getLayoutParams().height = Globals.SCREEN_HEIGHT/8;

        return view;
    }


}
















